package com.pororo.smartoff;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class SmartOffBroadcastReceiver extends BroadcastReceiver {
	final static String TAG = "SmartOffBroadcast";
	
	@SuppressLint("DefaultLocale")
	@Override
	public void onReceive(Context context, Intent intent) {
		//test
		Log.d(TAG, "intent action :" + intent.getAction());
		if ( SmartOffPreferences.checkInitialization() == false ) {
			SmartOffPreferences.setPreferences(context);
		}
		
		if ( intent.getAction().equals("android.intent.action.SCREEN_ON") ) {
			Log.i(TAG, "Action Screen ON");
			SmartOffPreferences.setBoolean("SaveTopPackage", true);
			String beforeTopPackageName = SmartOffPreferences.getString("TopPackageName");
			int beforeTopRunngingActivity = SmartOffPreferences.getInt("TopRunngingActivity");
			
			AppInfo topActivity = getTopActivity(context);
			String currentTopPackageName = topActivity.getName();
			int currentTopRunngingActivity = topActivity.getCount();
			
			if ( currentTopPackageName.toLowerCase().indexOf("clock") != -1 ) {
				return;
			}
			
			Log.d(TAG, "Before Activity : " + beforeTopPackageName + " Running : " + beforeTopRunngingActivity);
			Log.d(TAG, "Current Activity : " + currentTopPackageName + " Running : " + currentTopRunngingActivity);
			
			if ( topActivity == null || beforeTopPackageName == null || beforeTopRunngingActivity == -1  ) {
				return;
			}
			//App Detect case 1 : Different Package Name
			else if ( !beforeTopPackageName.equals(currentTopPackageName) ) {
				Log.i(TAG, "App Detect Case 1");

				Intent smartOffService = new Intent(context, SmartOffService.class);
				smartOffService.putExtra("TopActivityName", currentTopPackageName);
				context.startService(smartOffService);
			} 
			//App Detect case 2 : increased running activity
			else if ( beforeTopRunngingActivity < currentTopRunngingActivity ) {
				Log.i(TAG, "App Detect Case 2");

				Intent smartOffService = new Intent(context, SmartOffService.class);
				smartOffService.putExtra("TopActivityName", currentTopPackageName);
				context.startService(smartOffService);
			} 
		} else if ( intent.getAction().equals("android.intent.action.SCREEN_OFF") ) {
			Log.i(TAG, "Action Screen OFF");
					
			AppInfo topActivity = getTopActivity(context);
			
			boolean isSavePackage = SmartOffPreferences.getBoolean("SaveTopPackage");
			if ( isSavePackage ) {
				if (!topActivity.getName().equals("com.lge.ltecall")) {
					SmartOffPreferences.setString("TopPackageName", topActivity.getName());
					SmartOffPreferences.setInt("TopRunngingActivity", topActivity.getCount());
					Log.d(TAG, "Save Package : " + topActivity.getName() + " Running : " + topActivity.getCount());
				}
			}
			
			Intent smartOffService = new Intent(context, SmartOffService.class);
			context.stopService(smartOffService);
		} else if ( intent.getAction().equals("android.intent.action.USER_PRESENT") ) {
			Log.i(TAG, "Action User Present ( Keyguard is gone )");
			
			Intent smartOffService = new Intent(context, SmartOffService.class);
			context.stopService(smartOffService);
		} else if ( intent.getAction().equals("android.intent.action.BOOT_COMPLETED") ) {
			Log.i(TAG, "Action Boot Completed");
			
			if ( SmartOffPreferences.getBoolean("ServiceStatus") == true ) {
				Intent smartOffRegisterService = new Intent(context, SmartOffRegisterService.class);
				context.startService(smartOffRegisterService);
			}
		} 
		
/*
		else if ( intent.getAction().equals("android.intent.action.BATTERY_CHANGED") ) {
			int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 1);
			
			if ( status == BatteryManager.BATTERY_STATUS_FULL ) {
				// Database initialization when battery fully charged
				SmartOffDBHelper.updateAppCountZero(context);
			}
		}
*/		

	}
	
	AppInfo getTopActivity(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		//AppInfo appInfo = new AppInfo(0, 0);
		List<RunningTaskInfo> appList = am.getRunningTasks(99);
		AppInfo appInfo = new AppInfo(appList.get(0).topActivity.getPackageName(), appList.get(0).numRunning);
		
		for ( int i = 1; i < appList.size(); i++ ) {
			if ( appInfo.getName().equals(appList.get(i).topActivity.getPackageName()) ) {
				appInfo.setCount(appInfo.getCount() + appList.get(i).numRunning);
			}
		}
		return appInfo;
	}
	
	void printRunningProcess(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		
		for ( RunningAppProcessInfo ap : am.getRunningAppProcesses() ) {
			Log.d(TAG, "AppProcess : " + ap.processName);
		} 
	}
}
